package com.example.timing.trigger;

import com.example.timing.job.QuartzJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author :ld
 * @date :2021/7/9
 */
public class QuartzTrigger {
    public static void main(String[] args) {
        try {
            //1:创建调度器
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            //2:创建任务实例
            JobDetail jobDetail = JobBuilder.newJob(QuartzJob.class).
                    withIdentity("JobDetail_1").
                    build();

            //3:创建触发器Trigger
            Trigger trigger = TriggerBuilder.newTrigger().
                    withIdentity("Trigger_1").
                    withSchedule(SimpleScheduleBuilder
                            .simpleSchedule()
                            //每隔5秒触发一次
                            .withIntervalInSeconds(5)
                            .repeatForever()).
                    build();

            //4:触发器和任务关联
            scheduler.scheduleJob(jobDetail,trigger);

            //5:启动任务调度器
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
